<template>
  <!-- <el-alert :closable="false">
      <router-view />
  </el-alert>-->

  <div style="padding:30px;width:60%">
    <el-form
      v-loading="loading"
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="115px"
      class="demo-ruleForm"
    >
      <el-form-item v-model="ruleForm.bid" label="所属分公司" prop="name">
        <vxe-select v-if="level==4" v-model="ruleForm.bid" class="select" placeholder="请选择">
          <vxe-option v-for="( item , i ) in coms" :key="i" :value="item.id" :label="item.name" />
        </vxe-select>
        <vxe-select v-if="level==3" v-model="ruleForm.bid" class="select" placeholder="请选择分公司">
          <vxe-option :value="bid" :label="departnamex" />
        </vxe-select>
      </el-form-item>

      <el-form-item label="部门名称" prop="name">
        <el-col :span="22">
          <el-input v-model="ruleForm.name" />
        </el-col>
      </el-form-item>

      <el-form-item label="部门管理员" prop="person">
        <el-col :span="22">
          <el-input v-model="ruleForm.person" />
        </el-col>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-col :span="22">
          <el-input type="password" v-model="ruleForm.password" />
        </el-col>
      </el-form-item>

      <!-- 这里应该有个总公司的id的，可能是默认带了 -->
      <el-form-item label="管理员电话" prop="telephone">
        <el-col :span="22">
          <el-input v-model="ruleForm.telephone" />
        </el-col>
      </el-form-item>

      <!-- <el-form-item label="创建时间" required>
        <el-form-item prop="createTime">
          <el-col :span="22">
            <el-date-picker
        
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
              style="width: 100%;"
            />
          </el-col>
        </el-form-item>
      </el-form-item>-->

      <!--
      <el-form-item label="是否执行" prop="flag">
        <el-col :span="22">
          <el-input v-model="ruleForm.flag"></el-input>
        </el-col>
      </el-form-item>-->
      <!-- 这里应该有个合同的主键的id的，可能是默认带了 -->
      <!-- 这里应该有个部门的的id的，可能是默认带了 -->

      <el-form-item>
        <el-button type="primary" @click="submitForm()">立即创建</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// import { hqfgs, tjht } from '@/api/ht'
// eslint-disable-next-line no-unused-vars
import { getToken, gettype, getid, getlevel, getbid } from "@/utils/auth";
import { selectdown, add_depart } from "@/api/cid";
import request from "@/utils/request";
import axios from "axios";
import { get } from 'xe-utils/methods';
export default {
  name: "DepartAdd",

  data() {
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

      if (regMobile.test(value)) {
        return cb();
      }

      cb(new Error("请输入合法的手机号"));
    };
    return {
      bid:getbid(),
      level: getlevel(),
      list: null,
      value: "",
      departnamex:'',
      coms: [],
      loading: false,
      ruleForm: {
        cid: parseInt(getid()),
        bid: "",
        del: 0,
        name: "",
        password: "",
        person: "",
        telephone: ""
      },
      rules: {
        name: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
        person: [
          { required: true, message: "请输入管理员姓名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" }
          // { validator: checkMobile, trigger: "blur" }
          // { required: true, message: "请输入手机号", trigger: "blur" },
          // { min: 11, max: 11, message: "请输入11手机号", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // this.getList()
    this.selectdown();
    var that = this;
    if(getlevel()==3){
      that.departname()
    }
  },
  methods: {
    departname() {
      var that=this;
      request(`person/rolelist`, {
        method: "get",
        headers: {
          "Content-Type": "application/json",
        },
        params: { tokens: getToken() }
      })
        .then(res => {
          console.log(res);
          if (res.data.code == 1) {
              that.departnamex =res.data.object.branchname;
              console.log(that.departnamex)
          } else {
            console.log("set cid error!");
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    submitForm(e) {
      var that = this;
      console.log(this.ruleForm);
      let ruleForm2 = this.ruleForm;
      console.log(ruleForm2);
      this.$refs.ruleForm.validate(async valid => {
        console.log(valid);
        if (!valid) {
          return;
        }
        console.log(that.ruleForm.id);
        if (that.ruleForm.bid == "") {
          that.$message.error("请选择分公司！");
          return;
        }
        that.loading = true;
        request(`department/add`, {
          method: "post",
          headers: {
            "Content-Type": "application/json",
            token: getToken(),
            type: 1
          },
          data: ruleForm2
        })
          .then(res => {
            that.loading = false;
            console.log("res:", res);
            that.$message.success(res.data.message);
          })
          .catch(e => {
            that.loading = false;
            that.$message.error("添加失败！");
            console.log(e);
          });
      });
    },
    resetForm() {
      console.log("清空");
      this.ruleForm = {
        bid: "",
        cid: getid(),
        del: 0,
        name: "",
        password: "",
        person: "",
        telephone: ""
      };
      this.bid=''
    },
    selectdown() {
      this.loading = true;
      var that = this;
      var p = {
        tokens: getToken()
      };
      request(`branch/selectdown`, {
        method: "get",
        headers: { "Content-Type": "application/json" },
        params: p
      })
        .then(function(response) {
          that.loading = false;
          console.log(response);
          if (response.data.code == 1) {
            that.coms = response.data.object;
          } else {
            that.$message.error(response.data.message);
          }
        })
        .catch(error => {
          that.loading = false;
          console.log(error); //异常
        });
    }
    // getList() {
    //   tjht(this.ruleForm).then(response => {
    //     console.log(response.data)
    //     var data = response.data.object
    //     console.log(data)
    //     setToken(data.token)
    //     setid(data.id)
    //     settype(data.usertype)
    //     console.log(22222)
    //     this.$router.push('/')
    //   })
    // }
  }
};
</script>

<style scoped>
.select {
  z-index: 1 !important;
}
.sortable-ghost {
  opacity: 0.8;
  color: #fff !important;
  background: #42b983 !important;
}
/* .el-alert__content {
  width: 300px;
} */
</style>

<style scoped>
.icon-star {
  margin-right: 2px;
}
.drag-handler {
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d {
  margin-top: 15px;
}
/* .el-alert__content {
  width: 300px;
} */
</style>
